Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Valid Mountain Array

Problem Statement

Given an integer array arr, return true if arr is a valid mountain array. Otherwise, return false.

The array is called a valid mountain array if:

  • arr.length > 2
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Examples

Example 1:

  • Input: arr = [4, 5, 6, 7, 8, 7, 6, 5]
  • Expected Output: true
  • Justification: The elements increase from 4 to 8 and then decrease from 8 back to 5, forming a mountain shape.

Example 2:

  • Input: arr = [1, 2, 3, 4, 5]
  • Expected Output: false
  • Justification: The elements only increase and there's no decrease after a peak, hence not forming a mountain.

Example 3:

  • Input: arr = [9, 8, 7, 6, 5]
  • Expected Output: false
  • Justification: The elements decrease from the start without an initial increase, failing to form a mountain shape.

Solution

To solve this problem, we'll follow a two-phase approach that mirrors the structure of a mountain: ascending and descending. Initially, we'll iterate through the array, ensuring that each element is strictly larger than its predecessor, signifying an upward slope towards the peak. Upon reaching the peak, which is identified when the current element is no longer greater than the next, we transition to the descending phase. In this phase, we ensure that each subsequent element is strictly smaller, representing the downward slope of the mountain.

The algorithm succeeds if it finds both an ascending and descending sequence with at least one element in each, forming a valid mountain shape. This approach is effective because it closely follows the definition of a mountain array, requiring a strict increase followed by a strict decrease, and can be implemented efficiently.

Step-by-Step Algorithm

  1. Initialize an Index Variable: Start with an index variable, i, set to 0. This variable will be used to iterate through the array.

  2. Check for Ascending Order:

    • While i+1 is less than the array length and the current element is less than the next element (arr[i] < arr[i + 1]), increment i.
    • This loop checks for a strictly increasing sequence before the peak.
  3. Validate Peak Existence:

    • Ensure that the peak is not at the start or end of the array. If i is 0 or i equals arr.length - 1, the array does not form a mountain shape, so return false.
  4. Check for Descending Order:

    • After identifying the peak, continue checking the elements. While i+1 is less than the array length and the current element is greater than the next element (arr[i] > arr[i + 1]), increment i.
    • This loop ensures the sequence is strictly decreasing after the peak.
  5. Verify End of Array:

    • If i equals arr.length - 1 by the end of the process, it means we have a valid mountain array. Return true.
    • If not, the array does not form a valid mountain shape. Return false.

Algorithm Walkthrough

Let's consider the input: arr = [4, 5, 6, 7, 8, 7, 6, 5].

  1. Initialize i as 0.

  2. Ascending Order Check:

    • Step 1: i = 0, arr[i] = 4, arr[i + 1] = 5. Since 4 < 5, increment i to 1.
    • Step 2: i = 1, arr[i] = 5, arr[i + 1] = 6. Since 5 < 6, increment i to 2.
    • Step 3: i = 2, arr[i] = 6, arr[i + 1] = 7. Since 6 < 7, increment i to 3.
    • Step 4: i = 3, arr[i] = 7, arr[i + 1] = 8. Since 7 < 8, increment i to 4.
  3. Validate Peak Existence:

    • Now i = 4 and arr[i] = 8. The next element is 7, so we stop ascending. Since i is not 0 or arr.length - 1, we have a valid peak.
  4. Descending Order Check:

    • Step 5: i = 4, arr[i] = 8, arr[i + 1] = 7. Since 8 > 7, increment i to 5.
    • Step 6: i = 5, arr[i] = 7, arr[i + 1] = 6. Since 7 > 6, increment i to 6.
    • Step 7: i = 6, arr[i] = 6, arr[i + 1] = 5. Since 6 > 5, increment i to 7.
  5. Verify End of Array:

    • Now i = 7, which is arr.length - 1. We have successfully found a valid mountain array shape, so the algorithm returns true.
Image

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity:

The algorithm scans the array at most twice. The first scan is to check for the strictly increasing sequence until the peak is reached, and the second scan checks for the strictly decreasing sequence after the peak. Since both scans are linear and there are no nested iterations, the time complexity is , where n is the length of the array.

Space Complexity:

The algorithm uses a fixed amount of space. The space used does not scale with the input size, resulting in constant space complexity.

Valid Mountain Array (easy)
Set Mismatch (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity:

Space Complexity: